package com.yx.study.leet.primary.linkList;

/**
 * 环形链表
 * https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnwzei/
 */
public class HasCycle {
    public boolean hasCycle(ListNode head) {
        // 快慢指针
        if (head == null) {
            // 1. 链表为空
            return false;
        }
        // 快慢指针
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                // 快慢指针相遇 有环
                return true;
            }
        }
        return false;
    }
}
